package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.*;
import com.mxpio.erp.common.technology.vo.ItemWithSubExtendsVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService {

    @Autowired
    private ItemLeadTimePropService itemLeadTimePropSerivce;
    @Autowired
    private ItemPlanPropService itemPlanPropSerivce;
    @Autowired
    private ItemPurchasePropService itemPurchasePropSerivce;
    @Autowired
    private ItemStorePropService itemStorePropSerivce;

    @Autowired
    private ItemMaterialPropService itemMaterialPropService;

    @Override
    @Transactional(readOnly = true)
    public Result<ItemWithSubExtendsVO> queryItemWithExtendsByCode(String itemCode){
        Item item = getById(Item.class, itemCode);
        if (item==null){
            return Result.error("物料code未能匹配到数据！");
        }
        ItemWithSubExtendsVO extendsVO = new ItemWithSubExtendsVO();
        BeanUtils.copyProperties(item,extendsVO);

        ItemPropKey key = new ItemPropKey();
        key.setPropType("item");
        key.setCode(item.getItemCode());
        //关联物料子表扩展信息
        ItemLeadTimeProp itemLeadTimeProp = itemLeadTimePropSerivce.getById(ItemLeadTimeProp.class, key);
        extendsVO.setItemLeadTimeProp(itemLeadTimeProp);

        ItemPlanProp itemPlanProp = itemPlanPropSerivce.getById(ItemPlanProp.class, key);
        extendsVO.setItemPlanProp(itemPlanProp);

        ItemPurchaseProp itemPurchaseProp = itemPurchasePropSerivce.getById(ItemPurchaseProp.class, key);
        extendsVO.setItemPurchaseProp(itemPurchaseProp);

        ItemStoreProp itemStoreProp = itemStorePropSerivce.getById(ItemStoreProp.class, key);
        extendsVO.setItemStoreProp(itemStoreProp);

        ItemMaterialProp itemMaterialProp = itemMaterialPropService.getById(ItemMaterialProp.class, key);
        extendsVO.setItemMaterialProp(itemMaterialProp);
        return Result.OK(extendsVO);
    }

    @Override
    @Transactional
    public String deleteCheck(String itemCode) {
        String[] itemCodes = itemCode.split(",");
        StringBuilder msg = new StringBuilder();
        for(String key : itemCodes){
            //查询物料是否被bom引用
            List<BomInfo> bomInfos = JpaUtil.linq(BomInfo.class).equal("parentCode", key).list();
            if (CollectionUtils.isNotEmpty(bomInfos)){
                msg.append("物料【").append(key).append("】被Bom引用；");
            }
            //查询物料是否被bom明细引用
            List<BomInfo> bomInfoLineList = JpaUtil.linq(BomInfoLine.class).equal("itemCode", key).list();
            if (CollectionUtils.isNotEmpty(bomInfoLineList)){
                msg.append("物料【").append(key).append("】被Bom明细引用；");
            }
        }
        return msg.toString();
    }
}
